<?php
class ApiModule
{
	// public function art_list() {
	// 	global $_FANWE;
		
 //        $uid = intval($_GET['uid']);
 //        $cate_id = intval($_GET['cate']);
 //        $where = 'flag=0 AND status=1 AND jiage=0';
 //        $anli = $_GET['anli'] == 1 ? 1 : 0;
 //        $where .= ' AND anli='.$anli;

	// 	$orderkeys = array('sharenum', 'commentnum', 'priceup', 'pricedown', 'zannum');
	// 	$orders = array('sharenum' => 'share_num DESC', 'commentnum' => 'comment_count DESC', 'default' => 'sort ASC,id DESC', 'priceup' => 'price ASC', 'pricedown' => 'price DESC', 'zannum' => 'zai_num DESC');
	// 	$orderkey = in_array($_GET['order'], $orderkeys) ? $_GET['order'] : 'default';
	// 	$order = $orders[$orderkey];
		
 //        $sql = 'SELECT COUNT(id) FROM '.FDB::table('article').' WHERE '.$where;
 //        $count = FDB::resultFirst($sql);
 //        $page_args = array();
 //        $pager = buildPage('article/'.ACTION_NAME,$page_args,$count,$_FANWE['page'],10);
 //        $article_list = array();
 //        if($_FANWE['page'] <= $pager['page']) {
	//         $sql = 'SELECT * FROM '.FDB::table('article').' WHERE '.$where.' ORDER BY '.$order.' LIMIT '.$pager['limit'];
	//         $article_list = FDB::fetchAll($sql);
	//         if(!empty($article_list)){
	//             $cities = include './public/city.php';
	            
	//             $uids = array();
	//             foreach ($article_list as $key => $value) {
	//                 if($value['uid']) {
	//                     $uids[] = $value['uid'];
	//                 }
	                
	//                 $article_list[$key]['province_show'] = $cities['all'][$value['province']]['name'];
	//                 $article_list[$key]['city_show'] = $cities['all'][$value['city']]['name'];

	//                 $sj = time();
	//                 $article_list[$key]['hascoupon'] = ($value['price2'] > 0 && ($value['bsj']==0 || $value['esj']==0 || ($sj >= $value['bsj'] && $sj < $value['esj']))) ? 1 : 0;
	//                 $article_list[$key]['create_time'] = date('Y-m-d', $value['create_time']);
	//             }

	//             $uids = array_unique($uids);
	//             $sql = 'SELECT * FROM '.FDB::table('user').' WHERE uid IN ('.implode(',', $uids).')';
	//             $tmp = FDB::fetchAll($sql);
	//             $name_list = array();
	//             foreach ($tmp as $key => $value) {
	//                 $name_list[$value['uid']] = $value;
	//             }

	//             $sql = 'SELECT * FROM '.FDB::table('wogu_user_shop').' WHERE uid IN ('.implode(',', $uids).')';
	//             $tmp = FDB::fetchAll($sql);
	//             $shop_list = array();
	//             foreach ($tmp as $key => $value) {
	//                 $shop_list[$value['uid']] = $value;
	//             }

	//             foreach ($article_list as $key => $value) {
	//             	$article_list[$key]['author'] = $name_list[$value['uid']]['user_name'];
	//             	$article_list[$key]['shopinfo'] = $shop_list[$value['uid']];
	//             }
	//         }
 //        }

 //        echo json_encode($article_list);
	// }

    public function art_list() {
        global $_FANWE;
        
        $uid = intval($_GET['uid']);
        $cate_id = intval($_GET['cate']);
        $where = 'flag=0 AND status=1 AND jiage=0';
        if(!$uid) {
            $sql = 'SELECT * FROM '.FDB::table('goods_category').' WHERE cate_id = '.$cate_id.' AND status <>0';
            $r = FDB::fetchFirst($sql);
            if(!$r){
                exit;
            }
        
            $parent_id = $r['parent_id'] == 0 ? $cate_id : $r['parent_id'];
            $sql = 'SELECT * FROM '.FDB::table('goods_category').' WHERE parent_id='.$parent_id . ' AND status!=0 ORDER BY sort ASC';
            $cates = FDB::fetchAll($sql);
            $cate_ids = array($cate_id);
            foreach($cates as $v) {
                $cate_ids[] = $v['cate_id'];
            }
            $where .= ' AND cate_id IN('.($r['parent_id'] == 0 ? implode(',', $cate_ids) : $cate_id).')';
            
            //tags
            $sql = 'SELECT tag_id FROM '.FDB::table('goods_category_tags').' WHERE cate_id='.$parent_id;
            $gctags = FDB::fetchAll($sql);
            $tagids = $tag_list = array();
            foreach($gctags as $v) {
                $tagids[] = $v['tag_id'];
            }
            if(!empty($tagids)) {
                $sql = 'SELECT * FROM '.FDB::table('goods_tags').' WHERE tag_id IN ('.implode(',', $tagids).')';
                $tag_list = FDB::fetchAll($sql);
            }
        } else {
            $where .= ' AND uid='.$uid;
        }
        //价格
        if(isset($_GET['minprice']) && $_GET['minprice'] !== '') {
            $minprice = intval($_GET['minprice']);
            $where .= ' AND price>='.$minprice;
        }
        if(isset($_GET['maxprice']) && $_GET['maxprice'] !== '') {
            $maxprice = intval($_GET['maxprice']);
            $where .= ' AND price<='.$maxprice;
        }
        
        $keywordsshow = array();
        $keywords = empty($r['search_keywords']) ? array() : explode(' ', $r['search_keywords']);
        $ids = empty($_GET['ids']) && $_GET['ids'] !== '0' ? array() : explode(',', $_GET['ids']);
        foreach($ids as $k => $id) {
            if(!is_numeric($id))
                unset($ids[$k]);
        }
        if(!empty($ids)) {
            $where .= ' AND (1';
            foreach($ids as $id) {
                if(isset($keywords[$id])) {
                    $where .= " AND title LIKE '%{$keywords[$id]}%'";
                    $keywordsshow[$id] = array('id' => $id, 'keyword' => $keywords[$id], 'checked' => true);
                }
            }
            $where .= ')';
        }
        foreach($keywords as $key => $v) {
            if(!isset($keywordsshow[$key])) {
                $keywordsshow[$key] = array('id' => $key, 'keyword' => $v, 'checked' => false);
            }
        }
        
        //地区
        if(!empty($_GET['cityid'])) {
            $cityidarr = explode(',', $_GET['cityid']);
            foreach($cityidarr as $k => $v) {
                if(!is_numeric($v)) {
                    unset($cityidarr[$k]);
                }
            }
            if(!empty($cityidarr)) {
                $cityidstr = implode(',', $cityidarr);
                $where .= ' AND city IN('.$cityidstr.')';
            }
        } elseif(!empty($_GET['provinceid']) && is_numeric($_GET['provinceid'])) {
            $provinceid = intval($_GET['provinceid']);
            $where .= ' AND province='.$provinceid;
        }
        if(!empty($_GET['areaname'])) {
            $areaname = htmlspecialchars($_GET['areaname']);
            $areanamearr = explode(',', $areaname);
        }
        
        // 经验/案例
        $anli = intval($_GET['anli']);
        if(isset($_GET['anli'])) {
            $where .= ' AND anli='.$anli;
        }
        
        // V商家
        if(isset($_GET['vshop'])) {
            $where .= ' AND uid!=0';
        }

        $orderkeys = array('sharenum', 'commentnum', 'priceup', 'pricedown', 'zannum');
        $orders = array('sharenum' => 'share_num DESC', 'commentnum' => 'comment_count DESC', 'default' => 'sort ASC,id DESC', 'priceup' => 'price ASC', 'pricedown' => 'price DESC', 'zannum' => 'zai_num DESC');
        $orderkey = in_array($_GET['order'], $orderkeys) ? $_GET['order'] : 'default';
        $order = $orders[$orderkey];
        
        $sql = 'SELECT COUNT(id) FROM '.FDB::table('article').' WHERE '.$where;
        $count = FDB::resultFirst($sql);
        $page_args = array();
        $pager = buildPage('article/'.ACTION_NAME,$page_args,$count,$_FANWE['page'],10);
        $article_list = array();
        if($_FANWE['page'] <= $pager['page']) {
            $sql = 'SELECT * FROM '.FDB::table('article').' WHERE '.$where.' ORDER BY '.$order.' LIMIT '.$pager['limit'];
            $article_list = FDB::fetchAll($sql);
            if(!empty($article_list)){
                $cities = include './public/city.php';
                
                $uids = array();
                foreach ($article_list as $key => $value) {
                    if($value['uid']) {
                        $uids[] = $value['uid'];
                    }
                    
                    $article_list[$key]['province_show'] = $cities['all'][$value['province']]['name'];
                    $article_list[$key]['city_show'] = $cities['all'][$value['city']]['name'];

                    $sj = time();
                    $article_list[$key]['hascoupon'] = ($value['price2'] > 0 && ($value['bsj']==0 || $value['esj']==0 || ($sj >= $value['bsj'] && $sj < $value['esj']))) ? 1 : 0;
                }
                $uids = array_unique($uids);
                $sql = 'SELECT * FROM '.FDB::table('user').' WHERE uid IN ('.implode(',', $uids).')';
                $tmp = FDB::fetchAll($sql);
                $name_list = array();
                foreach ($tmp as $key => $value) {
                    $name_list[$value['uid']] = $value;
                }
                
                $sql = 'SELECT * FROM '.FDB::table('wogu_user_shop').' WHERE uid IN ('.implode(',', $uids).')';
                $tmp = FDB::fetchAll($sql);
                $shop_list = array();
                foreach ($tmp as $key => $value) {
                    $shop_list[$value['uid']] = $value;
                }

                foreach ($article_list as $key => $value) {
                    $article_list[$key]['author'] = $name_list[$value['uid']]['user_name'];
                    $article_list[$key]['shopinfo'] = $shop_list[$value['uid']];
                }
            }
        }

        echo json_encode(array(
            'cat' => $r,
            'list' => $article_list
        ));
    }

    public function art_view()
    {
        global $_FANWE;
        
        $id = $_FANWE['request']['id'];
        if(!is_numeric($id)){
            exit;
        }
        $share_detail = FS('Article')->getShareDetail($id);
        if($share_detail['flag'] == 1 || $share_detail['jb_num'] >= 100) {
            exit;
        }
        
        $home_uid = $share_detail['uid'];
        $home_user = FS('User')->getUserById($home_uid);
        if(!$home_user['status']) {
            exit;
        }
        $share_detail['home_user'] = $home_user;
        
        if($share_detail['anli']) {
            $sql = 'SELECT * FROM '.FDB::table('wogu_order')." WHERE artid={$share_detail['id']} AND flag=2 AND comment!=''";
            $comment_list = FDB::fetchAll($sql);
            $grade0 = $grade1 = $grade2 = $gradeall = 0;
            foreach($comment_list as $v) {
                $gradeall++;
                if($v['grade'] == 0) $grade0 ++;
                if($v['grade'] == 1) $grade1 ++;
                if($v['grade'] == 2) $grade2 ++;
            }
            
            $sql = 'SELECT * FROM '.FDB::table('wogu_user_shop').' WHERE uid='.$share_detail['uid'];
            $shopinfo = FDB::fetchFirst($sql);

            $sql = 'SELECT * FROM '.FDB::table('wogu_order').' WHERE artid='.$share_detail['id'].' AND isstop!=1 AND userid='.$_FANWE['uid'] . ' ORDER BY id DESC';
            $orderinfo = FDB::fetchFirst($sql);
        } else {
            //评论
    		$share_detail['comments'] = FS('Article')->getShareCommentList($id,'0,10');
    		//分享评论分页
    		$pager = buildPage('',array(),$share_detail['comment_count'],$_FANWE['page'],10);
        }

        $currcate = $_FANWE['cache']['goods_category']['all'][$share_detail['cate_id']];
        $parentcate = $_FANWE['cache']['goods_category']['all'][$currcate['parent_id']];
        $paytype = intval($_GET['paytype']);
        $orderid = intval($_GET['orderid']);

        $share_detail['create_time'] = date('Y-m-d', $share_detail['create_time']);
        $share_detail['content'] = str_replace('src="/ueditor/', 'src="http://www.huoguoge.com/ueditor/', $share_detail['content']);
        echo json_encode($share_detail);
    }
}